#include <windows.h>
#include <stdio.h>
#include <lmerr.h>
	
#define BUFSIZE 512
	
HMODULE hModule = NULL; // default to system source
    	
void init();
void uninit();
int getErrString(DWORD dwLastError,char * buf);
void printUsage();
	
int main(int argc,char ** argv)
{
	int errNo;
	char inputBuf[BUFSIZE];
	char strBuf[BUFSIZE];
	int	loopFlag;				/* loop flag */
		
	init();
	if( argc == 2 ) {
		errNo = atoi(argv[1]);
		if( errNo == 0 && strlen( argv[1] ) != 1 ){
			//Invalid input
			printf("Error Error No\n");
		} else {
			if( 0 == getErrString(errNo,strBuf) ){
				//Invalid input
				printf("Error Error No\n");
			} else {;
				printf("%s",strBuf);
			}
		}
		uninit();
		return 1;
	}
	
	printUsage();	
	
	if( argc == 1 ){
		printf("Type q to quit\n");
		loopFlag = 1;
		while(loopFlag) {
			memset(inputBuf,0x0,BUFSIZE);
			scanf("%s",inputBuf);
			if ( strlen( inputBuf ) == 1 && inputBuf[0] == 'q') {
				loopFlag = 0;
			} else {
				errNo = atoi(inputBuf);
				if( errNo == 0 && !(inputBuf[0] == 0 && inputBuf[0] == '0' )) {
					//Invalid input
					printf("Error Error No\n");
				} else {
					if( 0 == getErrString(errNo,strBuf) ){
						//Invalid input
						printf("Error Error No\n");
					} else {;
						printf("%s",strBuf);
					}
				}
			}
		}
	}
	
	uninit();
	
	return 0;
}

void init()
{
        hModule = LoadLibraryEx(
            TEXT("netmsg.dll"),
            NULL,
            LOAD_LIBRARY_AS_DATAFILE
            );
}

void uninit()
{
    //
    // If we loaded a message source, unload it.
    //
    if(hModule != NULL)
        FreeLibrary(hModule);
}

int getErrString(DWORD dwLastError,char * buf)
{
	HMODULE hModuleLocal = NULL;
    LPSTR MessageBuffer;
    DWORD dwBufferLength;

    DWORD dwFormatFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_IGNORE_INSERTS |
        FORMAT_MESSAGE_FROM_SYSTEM ;

    //
    // If dwLastError is in the network range, 
    //  load the message source.
    //

    if(dwLastError >= NERR_BASE && dwLastError <= MAX_NERR) {
    	hModuleLocal = hModule;
        if(hModule != NULL)
            dwFormatFlags |= FORMAT_MESSAGE_FROM_HMODULE;
    }

    //
    // Call FormatMessage() to allow for message 
    //  text to be acquired from the system 
    //  or from the supplied module handle.
    //

    if(dwBufferLength = FormatMessageA(
        dwFormatFlags,
        hModuleLocal, // module to get message from (NULL == system)
        dwLastError,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
        (LPSTR) &MessageBuffer,
        0,
        NULL
        ))
    {
        //
        // Output message string on stderr.
        //
        //WriteFile(
        //    GetStdHandle(STD_ERROR_HANDLE),
        //    MessageBuffer,
        //    dwBufferLength,
         //   &dwBytesWritten,
        //    NULL
        //    );
        strcpy(buf,MessageBuffer);

        //
        // Free the buffer allocated by the system.
        //
        LocalFree(MessageBuffer);
        return 1;
    }
	else
	{
    	return 0;
	}
}

void printUsage()
{
	printf("Print error message by error NO\n");
	printf("version 1.0\n");
	printf("\tReleased under GPL\n");
	printf("\tYou can send a mail to sevenever@gmail.com for source\n");
	printf("\tBy sevenever,bug report to sevenever@gmail.com\n");
	printf("\nUsage: geterr [errNo]\n");
}